# A Spam Comment Filter plugin by Hirotaka Ogawa (http://as-is.net/blog/)
#
# Release 0.11 (Jan 29, 2005)
#
# This software is provided as-is. You may use it for commercial or 
# personal use. If you distribute it, please keep this notice intact.
#
# Copyright (c) 2005 Hirotaka Ogawa

use strict;

# Set your spam pattern
our $CMOMMENT_PATTERN = '<h1>|<a\s'; # H1 or A elements
our $TBPING_PATTERN = '<h1>|<a\s';

# Choose a method for rejecting Spam Comments from:
# 'CommentFilter', 'CommentThrottleFilter', 'ShortError', 'LongError',
# 'Redirect', 'EvilRedirect'
our $COMMENT_METHOD = 'CommentFilter';

# Choose a method for rejecting Spam Pings from:
# 'TBPingFilter', 'TBPingThrottleFilter'
our $TBPING_METHOD = 'TBPingFilter';

# Set your Redirect URL (for 'Redirect' and 'EvilRedirect' methods)
our $URL = 'http://www.2ch.net/';

if (MT->can('add_plugin')) {
    require MT::Plugin;
    my $plugin = new MT::Plugin();
    $plugin->name("Quasi Spam Filter Plugin, v.0.10");
    $plugin->description("A Simple filter for Comment &amp; TBPing Spams");
    $plugin->doc_link("http://as-is.net/hacks/2005/01/quasi_spam_filter_plugin.html");
    MT->add_plugin($plugin);
}

our $comment_hdlrs = {
    'CommentFilter' => \&comment_filter_hdlr,
    'CommentThrottleFilter' => \&comment_throttle_filter_hdlr,
    'ShortError' => \&short_error_hdlr,
    'LongError' => \&long_error_hdlr,
    'Redirect' => \&redirect_hdlr,
    'EvilRedirect' => \&evil_redirect_hdlr
    };

our $tbping_hdlrs = {
    'TBPingFilter' => \&tbping_filter_hdlr,
    'TBPingThrottleFilter' => \&tbping_throttle_filter_hdlr
};

if ($COMMENT_METHOD eq 'CommentFilter' ||
    $COMMENT_METHOD eq 'CommentThrottleFilter') {
    MT->add_callback($COMMENT_METHOD, 10, 'Reject Spam Comments',
		     $comment_hdlrs[$COMMENT_METHOD]);
} elsif (defined $comment_hdlrs[$COMMENT_METHOD]) {
    *MT::App::Comments::pre_run = $comment_hdlrs[$COMMENT_METHOD];
}

if (defined $tbping_hdlrs[$TBPING_METHOD]) {
    MT->add_callback($TBPING_METHOD, 10, 'Reject Spam TBPings',
		     $tbping_hdlrs[$TBPING_METHOD]);
}

sub is_comment_spam {
    my ($text) = @_;
    return ($text =~ /$COMMENT_PATTERN/i);
}

sub is_tbping_spam {
    my ($excerpt) = @_;
    return ($text =~ /$TBPING_PATTERN/i);
}

sub comment_filter_hdlr {
    my ($eh, $app, $comment) = @_;
    return !is_comment_spam($comment->text);
}

sub comment_throttle_filter_hdlr {
    my ($eh, $app, $entry) = @_;
    my $q = $app->{query};
    return !is_comment_spam($q->param('text'));
}

sub short_error_hdlr {
    my $app = shift;
    my $q = $app->{query};
    my $mode = $q->param('__mode') || $app->{default_mode};
    return if $mode ne 'post';
    if (is_comment_spam($q->param('text'))) {
        $app->add_methods(post => sub { });
        $app->error("Spam Comment!");
    }
}

sub long_error_hdlr {
    my $app = shift;
    my $q = $app->{query};
    my $mode = $q->param('__mode') || $app->{default_mode};
    return if $mode ne 'post';
    if (is_comment_spam($q->param('text'))) {
        $app->add_methods(post => sub { $_[0]->handle_error("Spam Comments!") });
    }
}

sub redirect_hdlr {
    my $app = shift;
    my $q = $app->{query};
    my $mode = $q->param('__mode') || $app->{default_mode};
    return if $mode ne 'post';
    if (is_comment_spam($q->param('text'))) {
        $app->add_methods(post => sub { });
        $app->redirect($URL);
    }
}

sub evil_redirect_hdlr {
    my $app = shift;
    my $q = $app->{query};
    my $mode = $q->param('__mode') || $app->{default_mode};
    return if $mode ne 'post';
    if (is_comment_spam($q->param('text'))) {
        $app->add_methods(post => sub { });
        $app->redirect($q->param('url') || $URL);
    }
}

sub tbping_filter_hdlr {
    my ($eh, $app, $ping) = @_;
    return !is_tbping_spam($ping->excerpt);
}

sub tbping_throttle_filter_hdlr {
    my ($eh, $app, $tb) = @_;
    my $q = $app->{query};
    return !is_tbping_spam($q->param('excerpt'));
}

sub tbping_hdlr {
    my $app = shift;
    my $q = $app->{query};
    my $mode = $q->param('__mode') || $app->{default_mode};
    return if $mode ne 'ping';
    if (is_tbping_spam($q->param('excerpt'))) {
	$app->add_methods('ping' => sub { });
	$app->_response(Error => 'Spam TBPing!', Code => 403);
    }
}

1;
